import random
import time
import zmq
from kvsimple import KVMsg


def main():
    ctx = zmq.Context()
    updates = ctx.socket(zmq.SUB)
    updates.linger = 0
    updates.setsockopt(zmq.SUBSCRIBE, b'')
    updates.connect("tcp://localhost:5556")

    kvmap = {}
    sequence = 0
    while True:
        try:
            kvmsg = KVMsg.recv(updates)
            print(kvmsg.key, kvmsg.body)
            kvmsg.store(kvmap)
            sequence += 1
        except KeyboardInterrupt:
            break
    print("Interrupted %d messages" % sequence)


if __name__ == '__main__':
    main()